$(document).ready(function(){
    initLeftMenu();
    initURLChange();
    initSelector();
    initBtn();
});

function initLeftMenu(){
	$(".nav-header").click(function(){
		var that = $(this).find("span");
		if (that.hasClass("up-caret")){
			that.removeClass("up-caret");
		} else {
			that.addClass("up-caret");
		}
		$(this).siblings("li").toggle();
	});
	$(".nav.nav-list li+li").click(function(){
		$(".nav.nav-list li.active").removeClass("active");
		$(this).addClass("active");
	});
	$(".nav.nav-list li+li a").click(function(event){
		event.preventDefault(); 
		var link = $(this).attr("href");
		$(window).scrollTop($(link).offset().top-40);
	});
}

function initURLChange(){
	$(window).load(function(){
		urlpro();
	});
    window.onhashchange = function(){
    	urlpro();
	};
	window.onload = function(){
		urlpro();
	};
	window.onbeforeonload = function(){
    	urlpro();
	};
}

function urlpro(){
	var url = window.location.toString();
    var id = url.split("#")[1];
   	if(!id || $("#"+id).length == 0){
   		id = "step1-1";
   	}
	var t = $("#"+id).offset().top-40;
	$(window).scrollTop(t);

	$(".nav.nav-list li.nav-header span").removeClass("up-caret");
	$(".nav.nav-list li+li").hide();

	var that = $("[href='#"+id+"']").parent();
	$(".nav.nav-list li.active").removeClass("active");
	that.addClass("active");
	that.siblings("li.nav-header").find("span").addClass("up-caret");
	that.show();
	that.siblings().show();
}

function initSelector(){
	$(".input-append ul.dropdown-menu li a").click(function(event){
    	event.preventDefault(); 
		$(this).parents(".input-append").find("input")
		.val($(this).html());
	});
}

function initBtn(){
	$(".btn").click(function(event){
		event.preventDefault(); 
	});
	$(".toadd .btn.btn-blue").click(function(){
		var parent = $(this).parent();
		var toadd = parent.find(".opblock").clone(true);
		var deletebtn = $("<button class='btn btn-green'>delete</button>");
		toadd = $("<div class='toadd'></div>").append(toadd).append(deletebtn);
		parent.after(toadd);
	});
	$(".toadd .btn.btn-green").click(function(){
		$(this).remove();
	});
}